/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2022-12-02 17:04:14
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-08-23 16:25:16
 * @FilePath: \cxl-admin\src\router\limit.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { userStore } from '@/store/user.store'
import { routerStore } from '@/store/router.store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import router from '.'

class Limit {
    _userStore: any
    _routerStore: any
    registerRouteFresh = true
    permission = true
    constructor() {
        // this._router = router
        //初始化用户数据仓库
        this._userStore = userStore()
        //初始化路由数据仓库
        this._routerStore = routerStore()
    }

    init() {
        // 路由跳转前判断用户是否登录
        router.beforeEach((to, from, next) => {
            //页面顶部进度条加载开始
            NProgress.start()

            // console.log('router', to)

            //从store中取出token，判断是否为空 ，为空则跳转登录页面
            if (this._userStore.token === '') {
                //根据当前要跳转的页面是否是登录页面（login）,来判断是否传参
                if (to.name === 'login') {
                    next()
                } else {
                    next({ name: 'login' })
                }
                sessionStorage.clear()
            } else {
                // next()
                if (this._userStore.permission) {
                    if (!this._routerStore.routeArray.length) {
                        this._routerStore.getDynamicPermission().then(
                            (list: any[]) => {
                                list.forEach(item => {
                                    if (item.path === '' && item.children.length) {
                                        //item.path = item.children[0].path
                                    }
                                    router.addRoute(item)
                                })
                                if (list.length === 0) {
                                    this._userStore.permission = false
                                }
                                next({ ...to, replace: true })
                                // console.log('getDynamicPermission', router)
                                // next({
                                //     path: `${to.path}`
                                // })
                                NProgress.done()
                                // 设置顶部的动态菜单
                                this.setRoute(to)
                            },
                            () => {
                                next({ name: 'login' })
                                sessionStorage.clear()
                            }
                        )
                    } else {
                        if (to.name) {
                            next()
                        } else {
                            next({ name: 'index' })
                        }

                        // 设置顶部的动态菜单
                        // console.log('设置顶部的动态菜单', to)
                        this.setRoute(to)
                    }
                } else {
                    //没有权限
                    next()
                }
            }
        })
        router.afterEach(() => {
            //页面顶部进度条加载结束
            NProgress.done()
        })
    }

    setRoute = (to: any) => {
        if (to.name) {
            let title: any = ''
            if (to.name === 'menu-edit') {
                title = to.query.type === 'add' ? '新增菜单' : '菜单编辑'
            } else if (to.name === 'notice-edit') {
                title = to.query.type === 'create' ? '新增消息' : '消息编辑'
            } else if (to.name === 'invoice-look') {
                title = to.query.type === 'audit' ? '开票审核' : '开票详情'
            } else {
                title = to.meta.title
            }
            // console.log('redirectedFrom', to.redirectedFrom)
            if (!['/login', '/', ''].includes(to.path as string)) {
                this._routerStore.setHeaderRouter({
                    path: to.path,
                    title: title,
                    activePath: to.meta.hidden ? to.meta.activePath : '',
                    hidden: to.meta.hidden,
                    query: to.redirectedFrom ? to.redirectedFrom.query : to.query,
                    params: to.redirectedFrom ? to.redirectedFrom.params : to.params
                })
            }
        }
    }
}

export default Limit
